/*
 * File:   main.c
 * Author:  Dominik H�ussler
 *          Ruben Schwarz
 *
 * Created on 2. M�rz 2011, 09:30
 *
 * �nderungen:
 * 2011/03/02   Real-Time-Clock implementiert
 *              UART2 f�r DEBUG-Zwecke implementiert
 */

#include <p33Fxxxx.h>
//#include  <p33FJ256MC710.h>
#include <uart.h>
//#include <timer.h>

#include <stdlib.h>
#include "sensor_typedefs.h"
#include "communication.h"
#include "hardware_settings.h"
#include "sensor_sim.h"


_FOSCSEL(FNOSC_FRC);
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: XT Crystanl

_FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software


// Globale Variablen
ESensorAct g_actSensor = NO_SENSOR_ACTIV;

int g_sens4 = 0;
int g_sens4_alarm = 0;

void __attribute__((__interrupt__, auto_psv)) _T2Interrupt(void) {

    //CloseTimer2();
    IEC0bits.T2IE = 0;

    char input = getcUART1();

    if (!DataRdyUART1()) {
        switch (input) {
            case SENSOR_ADDR_0:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 0 aktiv");
#endif
                break;
            case SENSOR_ADDR_1:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 1 aktiv");
#endif
                break;
            case SENSOR_ADDR_2:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 2 aktiv");
#endif
                break;
            case SENSOR_ADDR_3:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 3 aktiv");
#endif

                sendSensorData(SENSOR_ADDR_3, DATA_VOLTAGE, 45, TRUE);
                break;
            case SENSOR_ADDR_4:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 4 aktiv");
#endif
                sendSensorData(SENSOR_ADDR_4, DATA_VOLTAGE, g_sens4, g_sens4_alarm);
                break;
            case SENSOR_ADDR_5:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 5 aktiv");
#endif
                sendSensorData(SENSOR_ADDR_5, DATA_CURRENT, 183, FALSE);
                break;
            case SENSOR_ADDR_6:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 6 aktiv");
#endif
                break;
            case SENSOR_ADDR_7:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 7 aktiv");
#endif
                break;
            case SENSOR_ADDR_8:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 8 aktiv");
#endif
                break;
            case SENSOR_ADDR_9:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 9 aktiv");
#endif
                break;
            case SENSOR_ADDR_10:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 10 aktiv");
#endif
                break;
            case SENSOR_ADDR_11:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 11 aktiv");
#endif
                break;
            case SENSOR_ADDR_12:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 12 aktiv");
#endif
                break;
            case SENSOR_ADDR_13:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 13 aktiv");
#endif
                break;
            case SENSOR_ADDR_14:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 14 aktiv");
#endif
                break;
            case SENSOR_ADDR_15:
#ifdef _DEBUG_
                putsUART2((unsigned int*) "Sensor 15 aktiv");
#endif
                break;
            default: break;
                //TODO: auf Steuersignale reagieren (0x5A - Rücksetzen z.B.)
        }
    }

    //Unbenoetigte Zeichen aus UART-Lesepuffer löschen
    while (DataRdyUART1()) {
        getcUART1();
    }

    IFS0bits.T2IF = 0; //Clear Timer Interrupt Flag
} //ISR Timer2



void __attribute__((__interrupt__, auto_psv)) _U1RXInterrupt(void) {

    //reset timer2 and enable interrupt
    TMR2 = 0;
    IFS0bits.T2IF = 0;
    IEC0bits.T2IE = 1;
    //T2CONbits.TON = 1;

    IFS0bits.U1RXIF = 0; //Flag löschen
}


int main(void) {
    /*
     * Init Peripherie
     */
    initClock();
    initUart();


    /* Set CPU IPL to 0, enable level 1-7 interrupts */
    /* No restoring of previous CPU IPL state performed here */
    SRbits.IPL = 0;

    /*
     * Konfiguriere Timer 2
     * bewirkt Idle-Line-Timeout (280us)
     */
    T2CON = 0;
    PR2 = IDLE_LINE_TIMEOUT;
    IFS0bits.T2IF = 0;
    IEC0bits.T2IE = 0;
    IPC1bits.T2IP = 1;
    TMR2 = 0;
    T2CONbits.TON = 1;

    /*
     Konfiguriere Port A als Digital Output
     */
    TRISAbits.TRISA0 = 0;
    //PORTA = 0;
    //initButtons();

    while (1) {
/*
        if (!PORTDbits.RD6) {
            g_sens4 = 125;
            g_sens4_alarm = 0;
        }
        if (!PORTDbits.RD7) {
            g_sens4 = 143;
            g_sens4_alarm = 1;
        }
*/
    }
    return 0;
}
